<template>
  <div class="supplier-items-container">
		<div class="header">
			<div class="search-container">
				<div class="search-wrapper">
					<div class="icon" @click="searchList">
            <img class="icon_search" src="https://img.wifenxiao.com/h5-wfx/images/common/search-icon.png" />
						<!-- <svg-icon icon-class="icon_search" /> -->
					</div>
					<input @confirm="searchList" v-model.trim="keyword" type="search" placeholder="请输入商品关键字" />
				</div>
				<!-- <div class="add-goods-btn" @click="Jump('supplierAddItem')">添加商品</div> -->
        <van-button  class="add-goods-btn" @click="Jump('supplierDrafts')" round color="linear-gradient(to right, #ffaf39, #ffaf39)">草稿商品</van-button>
        <van-button  class="add-goods-btn" @click="Jump('supplierAddItem')" round color="linear-gradient(to right, #FE1B49, #E90104)">添加商品</van-button>
			</div>
      <!-- <scroll
        class="wrapper"
        ref="wrapper"
        :startX="0"
        :click="true"
        :scrollX="true"
        :scrollY="false"
        :eventPassthrough="'vertical'"
      >  -->
        <!-- :data="arr" -->
      <div class="content">
        <span :class="[listParams.item_status == 'onsale' ? 'active':'']" @click="selectStatus('onsale')">
          <i>出售中</i>
          <em v-if="onsaleNum>0">({{onsaleNum}})</em>
        </span>
        <span :class="[listParams.item_status == 'onsku' ? 'active':'']" @click="selectStatus('onsku')">
          <i>仓库中</i>
          <em v-if="onskuNum>0">({{onskuNum}})</em>
        </span>
        <span :class="[listParams.item_status == 'wait_sale' ? 'active':'']" @click="selectStatus('wait_sale')">
          <i>待上架</i>
          <em v-if="waitSkuNum>0">({{waitSkuNum}})</em>
        </span>
        <span :class="[listParams.item_status == 'outstock' ? 'active':'']" @click="selectStatus('outstock')">
          <i>已售罄</i>
          <em v-if="outstockNum>0">({{outstockNum}})</em>
        </span>
        <span :class="[listParams.item_status == 'gys_audit' ? 'active':'']" @click="selectStatus('gys_audit')">
          <i>待审核</i>
          <em v-if="gysAuditNum>0">({{gysAuditNum}})</em>
        </span>
      </div>
      <sort-search :typeList="typeList" @item="selectItem"></sort-search>
		</div>
    <div class="goods-container">
      <div class="list-items" v-show="list.length>0">
        <van-list
            v-model="loading"
            :immediate-check="immediateCheck"
            :finished="finished"
            :error.sync="error"
            error-text="请求失败，点击重新加载"
            finished-text=""
            @load="onLoad"
          >
          <div v-for="(item, index) in list" :key="index" class="list clearfix">
            <div @click="linkTo(item.link_item)" class="img fl">
              <img :src="`${item.pic_url}480x480`" v-if="item.is_compress==1" alt />
              <img :src="`${item.pic_url}`" v-else alt />
            </div>
            <div class="g-flex fl">
              <p @click="linkTo(item.link_item)" class="title">{{item.title}}</p>
              <p>
                <span class="fz12" v-if="display_user_supplier_price==1">售价：<em>&yen;{{item.price}}</em>
                </span>
              </p>
              <p>
                <span class="fz12">供货价：<em>&yen;{{item.cost_price}}</em>
                </span>
                <s v-if="is_display_original_price==1" class="fz12">&yen;{{item.original_price}}</s>
              </p>
              <p>
                <span class="fz12">销量：<i>{{item.real_sales}}</i>
                </span>
                <span class="fz12" style="margin-left:4px;">库存：<i>{{item.num}}</i>
                </span>
              </p>
              <div>
                <!-- 是否允许供应商编辑：供应商可以对出售中、已售罄商品进行编辑操作。 -->
                <van-button class="edit-goods-btn" v-if=" is_allow_modify==1" @click="Jump('supplierAddItem',{itemId:item.item_id})" color="#F30C23" plain hairline round type="primary">编辑</van-button>
                <van-button class="edit-goods-btn" v-else-if="!(listParams.item_status=='onsale' || listParams.item_status=='outstock')" @click="Jump('supplierAddItem',{itemId:item.item_id})" color="#F30C23" plain hairline round type="primary">编辑</van-button>
                <van-button class="edit-goods-btn" v-if="listParams.item_status=='onsale' || listParams.item_status=='outstock'" @click="clickXiajia(item.item_id)"  color="linear-gradient(to right, #FE1B49, #E90104)" round >下架</van-button>
                <van-button class="edit-goods-btn" v-if="listParams.item_status=='onsku' || listParams.item_status=='wait_sale'" @click="clickShangjia(item.item_id)"  color="linear-gradient(to right, #FE1B49, #E90104)" round >上架</van-button>
              </div>
            </div>
          </div>
        </van-list>
      </div>
      <div v-show="!list.length && finished">
        <no-data :noData="noData"></no-data>
      </div>
    </div>
	</div>
</template>

<script>
import Vue from 'vue'
import { Dialog } from 'vant'
import { getSupplierItems, supplierItemXiajia } from '@/api/user/gys/gysApi'
import Scroll from '@/components/scroll/index'
import noData from '@/components/noData/index'
import sortSearch from '@/views/item/itemList/listWrapper/sortSearch/index'
import { openPage } from '@/utils/utils'

export default Vue.extend({
  name: 'index',
  components: {
    noData,
    Scroll,
    sortSearch
  },
  data() {
    return {
      noData: {
        img: 'https://img.wifenxiao.com/h5-wfx/images/no_order.png',
        text: '暂无数据',
        marginTop: '30px'
      },
      // item_status: 'onsale',
      onsaleNum: '', // 出售中数量
      onskuNum: '', // 仓库中数量
      outstockNum: '', // 已售罄数量
      gysAuditNum: '', // 待审核数量
      waitSkuNum: '', // 待上架数量
      display_user_supplier_price: '0',
      is_display_original_price: '0',
      is_allow_modify: '0',
      keyword: '',
      immediateCheck: false,
      loading: false,
      finished: false,
      error: false,
      listParams: {
        p: 1,
        type: 1,
        item_status: 'onsale', // onsale出售中onsku仓库中outstock已售罄gys_audit待审核
        title: '',
        order: '' // priceUp原价升序priceDown原价降序salesDown销量降序salesUp销量升序newUp库存升序newDown库存降序
      },
      list: [],
      typeList: [
        {
          label: '默认',
          active: true,
          type: 0,
          prop: 'default',
          sort: 'none'
        },
        { label: '价格', active: false, type: 1, prop: 'price', sort: '' },
        { label: '销量', active: false, type: 2, prop: 'sales', sort: '' },
        { label: '库存', active: false, type: 3, prop: 'num', sort: '' }
        /* {
          label: '',
          active: false,
          type: 4,
          prop: 'default',
          sort: 'none'
        } */
      ]
    }
  },
  methods: {
    Jump(url, data) {
      this.$JumpPath(this, url, data)
    },
    linkTo(link) {
        openPage(link)
    },
    init() {
      this.$loadingWrap.show()
      setTimeout(() => {
        this.$loadingWrap.close()
      }, 50000)
      this.listParams.p = 1
      this.finished = false
      this.list = []
      getSupplierItems(this.listParams).then(res => {
        this.$loadingWrap.close()
        if (res.status == 1) {
          const resData = res.data
          this.onsaleNum = resData.onsale
          this.onskuNum = resData.onsku
          this.outstockNum = resData.outstock
          this.gysAuditNum = resData.gys_audit
          this.waitSkuNum = resData.waitSkuNum
          this.display_user_supplier_price = resData.display_user_supplier_price
          this.is_display_original_price = resData.is_display_original_price
          this.is_allow_modify = resData.is_allow_modify
          this.list = resData.lists
          if (this.list <= 0) {
            this.finished = true
          }
          this.listParams.p++
        } else {
          this.$Error(res.msg)
        }
      })
    },
    onLoad() {
      if (this.finished) return false
      getSupplierItems(this.listParams).then(res => {
        this.loading = false
        if (res.data.lists && res.data.lists.length) {
          this.list.push(...res.data.lists)
          this.listParams.p++
        } else {
          this.finished = true
        }
      }).catch(() => {
        this.error = true
      })
    },
    // 搜索
    searchList() {
      this.listParams.title = this.keyword
      this.init()
    },
    // 商品状态
    selectStatus(status) {
      this.listParams.item_status = status
      // 点击切换的时候不管怎么都要将滑动加载重新开启
      this.finished = false
      this.init(this.listParams)
    },
    // 排序方式
    selectItem({ sort, prop, type }) {
      if (type == 0) {
        this.listParams.order = ''
      } else if (type == 1) {
        // 价格排序
        if (sort == 'ascending') {
          this.listParams.order = 'priceUp'
        } else if (sort == 'descending') {
          this.listParams.order = 'priceDown'
        }
      } else if (type == 2) {
        // 销量排序
        if (sort == 'ascending') {
          this.listParams.order = 'salesUp'
        } else if (sort == 'descending') {
          this.listParams.order = 'salesDown'
        }
      } else if (type == 3) {
        // 库存排序
        if (sort == 'ascending') {
          this.listParams.order = 'newUp'
        } else if (sort == 'descending') {
          this.listParams.order = 'newDown'
        }
      }
      // 点击切换的时候不管怎么都要将滑动加载重新开启
      this.finished = false
      this.init()
    },
    // 下架商品
    clickXiajia(id) {
      Dialog.confirm({
        message: '确定下架该商品？',
        className: 'common-confirm',
        closeOnClickOverlay: true
      }).then(() => {
        this.$loadingWrap.show()
        setTimeout(() => {
          this.$loadingWrap.close()
        }, 10000)
        supplierItemXiajia({ id }).then(res => {
          this.$loadingWrap.close()
          if (res.status == 1) {
            this.init()
          } else {
            this.$Error(res.msg)
            this.init()
          }
        })
        // on confirm
      }).catch(() => {
        // on cancel
      })
    },
    /**
     * 上架商品
     */
    clickShangjia(id) {
      Dialog.confirm({
        message: '确定上架该商品？',
        className: 'common-confirm',
        closeOnClickOverlay: true
      }).then(() => {
        this.$loadingWrap.show()
        setTimeout(() => {
          this.$loadingWrap.close()
        }, 10000)
        supplierItemXiajia({
          id,
          act: 'shangjia'
        }).then(res => {
          this.$loadingWrap.close()
          if (res.status == 1) {
            this.init()
          } else {
            this.$Error(res.msg)
            this.init()
          }
        })
        // on confirm
      }).catch(() => {
        // on cancel
      })
    }
  },
  created() {
    this.init()
  },
  mounted() {
      // TODO 小程序触底加载更多
      window.addEventListener('reachbottom', () => {
        this.onLoad()
      })

      // TODO 监听添加或编辑商品
      window.$$subscribe('refreshList', data => {
        this.init()
      })
  },
  beforeDestroy() {
    window.$$unsubscribe('refreshList')
  }
})
</script>

<style lang="scss">
@import "src/styles/variables";
@import "src/styles/mixin";
.supplier-items-container{
  .header{
    padding:25px;
    background:#fff;
  }
  .search-container{
    display:flex;
    .search-wrapper{
      flex:1;
      position:relative;
      height: 72px;
      border-radius: 66px;
      font-size: 0;
      background: #F7F7F7;
      .icon {
        position:absolute;
        left:28px;
        color:#999999;
        font-size: 32px;
        top: 22px;
        margin-right: 10px;
        .icon_search{
          width:28px;
          height:28px;
        }
      }
      @include placeColor(#999);
      input {
        display: inline-block;
        width:100%;
        height: 72px;
        font-size: 24px;
        padding:15px 70px;
        border:0;
        background: transparent;
      }
    }
    .add-goods-btn{
      width: 160px;
      height:72px;
      font-size: 26px;
      margin-left:20px;
    }
  }
  .content{
    display:flex;
    justify-content: space-around;
    font-size:26px;
    span{
      display:inline-block;
      position:relative;
      font-family: PingFang SC;
      line-height:100px;
    }
    .active{
      font-size: 28px;
      i,em{
        font-weight: bold;
      }
      &::after{
        content:'';
        width:54px;
        height:5px;
        position:absolute;
        bottom:10px;
        left:50%;
        margin-left:-27px;
        @include gradient(right,RGBA(242, 18, 40, 1),RGBA(242, 18, 40, .1));
      }
    }
  }
  .goods-container{
    margin:20px 0;
    .edit-goods-btn{
      display: inline-block;
      font-family: PingFang SC;
      font-size: 24px;
      height: 50px;
      line-height: 48px;
      margin-left: 10px;
      padding: 0 36px;
      border-radius:50px;
      &:first-child{
        margin-left:0;
      }
    }
    .list-items {
      background-color: #fff;
      .list {
        display:flex;
        padding: 20px;
        background: #fff;
        border-bottom: 1px solid #EDEDED;
        .img {
          display: inline-block;
          width: 215px;
          height: 215px;
          overflow: hidden;
          position: relative;
          border-radius: 12px;
          img {
            display:block;
            max-width: 100%;
            width: 100%;
            border-radius: 12px;
          }
          span {
            display: inline-block;
            overflow: hidden;
            position: absolute;
            top: 0px;
            left: 0px;
            color: #fff;
            background: #e4393c;
            padding: 2px 6px;
          }
        }
        .g-flex {
          flex:1;
          width: 475px;
          margin-left: 20px;
          display: flex;
          flex-direction: column;
          justify-content: space-between;
          p{
            font-family: PingFang SC;
            font-size: 24px;
            color: #333;
            line-height: 1;
            margin-bottom: 4px;
          }
          .title {
            font-weight: bold;
            @include showEllipsis;
          }
          .fz12{
            line-height: 1;
            color:#666;
            em{
              color:#F21228;
            }
            i{
              color: #FF8C01;
            }
          }
        }
      }
    }
  }
}
</style>
